<?php
class user
{
	function logout()
	{
		$_SESSION['user_nickname'] = null;
		$_SESSION['user_email'] = null;
		$_SESSION['user_id'] = null;
		/*setcookie('user_nickname', null, 0, "/");
		setcookie('user_email', null, 0, "/");
		setcookie('user_id', null, 0, "/");*/
		setcookie('user_logged', null, 0, "/");
		return true;
	}
	
	function register($user_email, $user_nickname, $user_passwd)
	{
		$result = sys::sql("insert into users (user_email, user_nickname, user_password, create_time)"
			." values ('%s', '%s', '%s', '%s');",
			trim($user_email), trim($user_nickname), $user_passwd, date('Y-m-d H:i:s'));
		return $result;
	}
	
	function login($login, $passwd)
	{
		$result = sys::sql("select * from users where "
			."(user_nickname='%s' or user_email='%s' or user_id='%s')"
			." and user_password='%s';", $login, $login, $login, $passwd);
		
		if ($result[0])
		{
			$_SESSION['user_nickname'] = $result[0]['user_nickname'];
			$_SESSION['user_email'] = $result[0]['user_email'];
			$_SESSION['user_id'] = $result[0]['user_id'];
			$time = time()+3600*24;
			setcookie('user_nickname', $result[0]['user_nickname'], $time, "/");
			setcookie('user_email', $result[0]['user_email'], $time, "/");
			setcookie('user_id', $result[0]['user_id'], $time, "/");
			setcookie('user_logged', 1, $time, "/");
		}
		
		return $result[0];
	}
	
	function isLogged()
	{
		return $_SESSION['user_nickname']&&$_SESSION['user_id'];
	}
	
	function getNickname()
	{
		return $_SESSION['user_nickname'];
	}
	
	function getEmail()
	{
		return $_SESSION['user_email'];
	}
	
	function getId()
	{
		return $_SESSION['user_id'];
	}
	
	function getById($user_id)
	{
		$users = sys::sql("select * from users where user_id='%s';",
			$user_id
			);
		return $users[0];
	}

}